from lin.enums import GroupLevelEnum

from app import UserGroupModel
from app.serve.user.groups import GroupServe
from app.serve.user.user import UserServe
from app.serve.wtrees.db import db


def init(force=False):
    db.create_all()
    root_group = GroupServe()
    root_group.name = "Root"
    root_group.info = "超级用户组"
    root_group.level = GroupLevelEnum.ROOT.value
    db.session.add(root_group)
    # 创建一个超级管理员
    root = UserServe()
    root.username = "root"
    db.session.add(root)
    db.session.flush()
    root.password = "123456"
    # root用户 and  超级管理员分组 对应关系写入user_group表中
    user_group = UserGroupModel()
    user_group.user_id = root.id
    user_group.group_id = root_group.id
    db.session.add(user_group)
    db.session.commit()
